package org.liqingye.es.rest;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.elasticsearch.rest.RestStatus;
import org.liqingye.es.page.Page;
import org.liqingye.es.service.BaseSearchService;
import org.liqingye.es.utils.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/search")
@SuppressWarnings("unchecked")
public class SearchRestController {

    @Autowired
    private BaseSearchService searchService;

    @RequestMapping("/addIndexDataMap")
    public JsonResult<RestStatus> addIndexJsonMap(@RequestParam(value = "index") String index,
            @RequestParam(value = "id") String id, @RequestBody Map<String, Object> jsonMap, HttpServletRequest request,
            HttpServletResponse response) throws Exception {
        RestStatus restStatus = searchService.indexRequest(index, id, jsonMap);
        if (RestStatus.OK.getStatus() == restStatus.getStatus()
                || RestStatus.CREATED.getStatus() == restStatus.getStatus()) {
            return new JsonResult<RestStatus>(true, "数据请求成功", restStatus);
        }
        return new JsonResult<RestStatus>(false, "数据请求失败", restStatus);
    }

    @RequestMapping("/addIndexDataJson")
    public JsonResult<RestStatus> addIndexDataJson(@RequestParam(value = "index") String index,
            @RequestParam(value = "id") String id, @RequestParam String jsonStr) throws Exception {
        RestStatus restStatus = searchService.indexRequest(index, id, jsonStr);
        if (RestStatus.OK.getStatus() == restStatus.getStatus()
                || RestStatus.CREATED.getStatus() == restStatus.getStatus()) {
            return new JsonResult<RestStatus>(true, "数据请求成功", restStatus);
        }
        return new JsonResult<RestStatus>(false, "数据请求失败", restStatus);
    }

    @RequestMapping("/deleteIndexById")
    public JsonResult<RestStatus> deleteIndexById(@RequestParam(value = "index") String index,
            @RequestParam(value = "id") String id) throws Exception {
        RestStatus restStatus = searchService.deleteResponse(index, id);
        if (RestStatus.OK.getStatus() == restStatus.getStatus()) {
            return new JsonResult<RestStatus>(true, "数据请求成功", restStatus);
        }
        return new JsonResult<RestStatus>(false, "数据请求失败", restStatus);
    }

    @RequestMapping("/deleteIndexAll")
    public JsonResult<Boolean> deleteIndexAll(@RequestParam(value = "index") String index) throws Exception {
        boolean bl = searchService.deleteResponse(index);
        return new JsonResult<Boolean>(true, "数据请求成功", bl);
    }

    @RequestMapping("/updateIndexDataJson")
    public JsonResult<RestStatus> updateIndexData(@RequestParam(value = "index") String index,
            @RequestParam(value = "id") String id, @RequestParam String jsonStr) throws Exception {
        RestStatus restStatus = searchService.updateRequest(index, id, jsonStr);
        if (RestStatus.OK.getStatus() == restStatus.getStatus()) {
            return new JsonResult<RestStatus>(true, "数据请求成功", restStatus);
        }
        return new JsonResult<RestStatus>(false, "数据请求失败", restStatus);
    }

    @RequestMapping("/updateIndexDataMap")
    public JsonResult<RestStatus> updateIndexData(@RequestParam(value = "index") String index,
            @RequestParam(value = "id") String id, @RequestBody Map<String, Object> jsonMap) throws Exception {
        RestStatus restStatus = searchService.updateRequest(index, id, jsonMap);
        if (RestStatus.OK.getStatus() == restStatus.getStatus()) {
            return new JsonResult<RestStatus>(true, "数据请求成功", restStatus);
        }
        return new JsonResult<RestStatus>(false, "数据请求失败", restStatus);
    }

    @RequestMapping("/queryIndexById")
    public JsonResult<Map<String, Object>> queryIndexById(@RequestParam(value = "index") String index,
            @RequestParam(value = "id") String id) throws Exception {
        Map<String, Object> map = searchService.getRequest(index, id);
        return new JsonResult<Map<String, Object>>(true, "数据请求成功", map);
    }

    @RequestMapping("/queryIndexByKeyword")
    public JsonResult<List<Map<String, Object>>> queryIndexByKeyword(@RequestParam(value = "index") String index,
            @RequestParam(value = "keyword") String keyword, @RequestBody Map<String, Object> jsonMap,
            @RequestParam(value = "from") int from, @RequestParam(value = "size") int size) throws Exception {
        List<String> fields = (List<String>) jsonMap.get("fields");
        List<String> descNames = (List<String>) jsonMap.get("descNames");
        List<Map<String, Object>> maplist = searchService.query(index, keyword, fields, from, size, descNames);
        return new JsonResult<List<Map<String, Object>>>(true, "数据请求成功", maplist);
    }

    @RequestMapping("/queryHighLightBykeyword")
    public JsonResult<List<Map<String, Object>>> queryHighLightBykeyword(@RequestParam(value = "index") String index,
            @RequestParam(value = "keyword") String keyword, @RequestBody Map<String, Object> jsonMap,
            @RequestParam(value = "from") int from, @RequestParam(value = "size") int size) throws Exception {
        List<String> fields = (List<String>) jsonMap.get("fields");
        List<String> descNames = (List<String>) jsonMap.get("descNames");
        List<Map<String, Object>> maplist = searchService.queryHighLightBykeyword(index, keyword, fields, from, size,
                descNames);
        return new JsonResult<List<Map<String, Object>>>(true, "数据请求成功", maplist);
    }

    @RequestMapping("/queryAllByKeyword")
    public JsonResult<List<Map<String, Object>>> queryAllByKeyword(@RequestParam(value = "index") String index,
            @RequestParam(value = "keyword") String keyword, @RequestBody Map<String, Object> jsonMap)
            throws Exception {
        List<String> fields = (List<String>) jsonMap.get("fields");
        List<String> descNames = (List<String>) jsonMap.get("descNames");
        List<Map<String, Object>> maplist = searchService.query(index, keyword, fields, descNames);
        return new JsonResult<List<Map<String, Object>>>(true, "数据请求成功", maplist);
    }

    @RequestMapping("/queryAllHighLightBykeyword")
    public JsonResult<List<Map<String, Object>>> queryAllHighLightBykeyword(@RequestParam(value = "index") String index,
            @RequestParam(value = "keyword") String keyword, @RequestBody Map<String, Object> jsonMap)
            throws Exception {
        List<String> fields = (List<String>) jsonMap.get("fields");
        List<String> descNames = (List<String>) jsonMap.get("descNames");
        List<Map<String, Object>> maplist = searchService.queryHighLightBykeyword(index, keyword, fields, descNames);
        return new JsonResult<List<Map<String, Object>>>(true, "数据请求成功", maplist);
    }

    @RequestMapping("/queryHighLightPageBykeyword")
    public JsonResult<Page<Map<String, Object>>> queryHighLightPageBykeyword(
            @RequestParam(value = "index") String index, @RequestParam(value = "keyword") String keyword,
            @RequestBody Map<String, Object> jsonMap, @RequestParam(value = "pageNum") int pageNum,
            @RequestParam(value = "pageSize") int pageSize) throws Exception {
        List<String> fields = (List<String>) jsonMap.get("fields");
        List<String> descNames = (List<String>) jsonMap.get("descNames");
        Page<Map<String, Object>> maplist = searchService.queryHighLightPageBykeyword(index, keyword, fields, pageNum,
                pageSize, descNames);
        return new JsonResult<Page<Map<String, Object>>>(true, "数据请求成功", maplist);
    }

}
